/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.clicarros.controle.usuario;

import br.com.clicarros.dominio.usuario.Usuario;
import br.com.clicarros.fachada.IFachada;
import br.com.clicarros.fachada.implementacao.Fachada;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.Principal;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Map;
import javax.servlet.AsyncContext;
import javax.servlet.DispatcherType;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Part;

/**
 *
 * @author Bruno
 */
@WebServlet(name = "ControleUsuario", urlPatterns = {"/ControleUsuario"})
public class ControleUsuario extends HttpServlet {

    private IFachada fachada = new Fachada();
  
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
               
        if(request.getParameter("cadastrar") != null){
            
            Usuario u = new Usuario();
            
            u.setEmail(request.getParameter("email"));
            u.setNome(request.getParameter("nome"));
            u.setSenha(request.getParameter("senha"));
            u.setTelefone(request.getParameter("telefone"));
            
            fachada.inserir(u);
            
            request.getSession().setAttribute("usuarioLogado", u);
            
            redireciona(request, response, "");
            
        }
    
    }
    
    
        private void redireciona(HttpServletRequest request,HttpServletResponse response, String mensagem) throws ServletException, IOException{
        
        if(mensagem != null){
            request.setAttribute("mensagem", "<div id=mensagem>"+mensagem+"</div>");
        }
      
        response.sendRedirect("/Cli-Carros/ControleAnuncio?listarAnuncios");
        
    }
    
}
